#coding:utf-8
'''
Created on 2015年4月20日

@author: leoZ
'''
from com.cyou.com.DBUtils import DBUtils
from com.cyou.repository.EtlJobSource import EtlJobSourceDao
from com.cyou.repository.EtlFtpSource import EtlFtpSourceDao
class EtlJobEntity(object):
    def __init__(self,etlSystemName,etlJobName,frequency,jobType,enable,lastJobStatus,myId):
        self.etlSystemName = etlSystemName
        self.etlJobName = etlJobName
        self.frequency = frequency
        self.jobType = jobType
        self.enable = enable
        self.lastJobStatus = lastJobStatus
        self.myId = myId
#Dao
class EtlJobDao(object):
    def loadAll(self):
        connection = DBUtils().getConnection()
        cursor = connection.cursor()
        cursor.execute("select * from etl_job where ETLSYSTEMNAME = 'HQL'")
        cursor.rowfactory = EtlJobEntity
        return cursor.fetchall()
    #获取job通过etlSystemName
    def loadAllBySystemName(self, etlSystemName):
        sql = ("select * from etl_job where ETLSYSTEMNAME = '%s'") % (etlSystemName)
        return self._loadAll(sql)
    #执行获取类查询的通用方法
    def _loadAll(self, sql):
        connection = DBUtils().getConnection()
        cursor = connection.cursor()
        cursor.execute(sql)
        cursor.rowfactory = EtlJobEntity
        return cursor.fetchall()
    #更新job的完成装态
    def updateJobJobStatus(self, status, etlSystemName, etlJobName):
        connection = DBUtils().getConnection()
        cursor = connection.cursor()
        sql = "update EULER.ETL_JOB x set x.LAST_JOBSTATUS = '%s' where x.ETLSYSTEMNAME = '%s' and x.ETLJOBNAME = '%s'" %(status, etlSystemName, etlJobName)
        #print sql
        cursor.execute(sql)
        connection.commit()
        connection.close()
    #更新job装态为完成
    #不建议使用，可以通过updateJobJobStatus来完成。
    def updateJobFinish(self, etlSystemName, etlJobName):
        connection = DBUtils().getConnection()
        cursor = connection.cursor()
        sql = "update EULER.ETL_JOB x set x.LAST_JOBSTATUS = 'Done' where x.ETLSYSTEMNAME = '%s' and x.ETLJOBNAME = '%s'" %(etlSystemName, etlJobName)
        print sql
        cursor.execute(sql)
        connection.commit()
        connection.close()
if __name__ == "__main__":
    #测试方法 
    job = EtlJobDao()
    job.updateJobFinish("HQL", "UC_STDOUT")
    
    exit(0)
    jobSource = EtlJobSourceDao()
    ftpSource = EtlFtpSourceDao()
    
    jobs = job.loadAll()
    for job in jobs:
        if job.lastJobStatus != "Ready":
            exit(0)
        etlSystemName = job.etlSystemName
        etlJobName = job.etlJobName
        print etlSystemName, etlJobName
        jobSources = jobSource.loadAllJobSourceByJobName(etlSystemName,etlJobName)
        for source in jobSources:
            ftps = ftpSource.loadAllByJobSource(source)
            for ftp in ftps:
                print ftp.ftpFilePath